package com.zsj.util;

import org.apache.commons.codec.binary.Hex;
import org.springframework.util.StringUtils;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;


/**
 * @Description:
 * @Author:
 * @Date:
 */
public class Md5Util {


    public static String createMD5ByStr(String str) throws NoSuchAlgorithmException, UnsupportedEncodingException {
        return createMD5ByStr(str, "UTF-8");
    }

    public static String createMD5ByStr(String str, String charset) throws NoSuchAlgorithmException, UnsupportedEncodingException {
        MessageDigest digest = MessageDigest.getInstance("MD5");
        if (StringUtils.isEmpty(charset)) {
            return Hex.encodeHexString(digest.digest(str.getBytes()));
        }
        return Hex.encodeHexString(digest.digest(str.getBytes(charset)));
    }
}
